<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
</head>
<body>

<div class="ares-position-absolute ares-absolute-tr">
    <input type="text"
           class="search-key layui-input ares-d-inline-block ares-w-auto ares-align-middle"
           placeholder="患者编号 | 姓名">
    <button class="search-btn layui-btn ares-align-middle">查询</button>
    <div class="page-tools layui-btn-group">
        <a class="layui-btn" href="#/referral/fast">快速转诊</a>
    </div>
</div>

<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this" data-type="out">转出患者</li>
        <li data-type="in">转入患者</li>
    </ul>
    <div class="layui-tab-content ares-px-0">
        <div class="layui-tab-item layui-show">
            <table class="layui-hide" id="table-out-patient" lay-filter="table-out-patient"></table>
            <div id="pagination-out-patient"></div>
        </div>
        <div class="layui-tab-item">
            <table class="layui-hide" id="table-in-patient" lay-filter="table-in-patient"></table>
            <div id="pagination-in-patient"></div>
        </div>
    </div>
</div>

<script type="text/html" id="tableBar-transfer-out">
    <button class="layui-btn layui-btn-xs" lay-event="check">病历</button>
    {{#  if(d.status == 1){ }}
    <button class="layui-btn layui-btn-xs" lay-event="assign">指定医院</button>
    {{#  } }}
</script>
<script type="text/html" id="tableBar-transfer-in">
    <button class="layui-btn layui-btn-xs" lay-event="check">病历</button>
   <!-- {{#  if(d.status == 2){ }}
    <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="created">已建</button>
    {{# } else{ }}
    <button class="layui-btn layui-btn-xs" lay-event="create">新建</button>
    {{#  } }}-->
</script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    var init;
    layui.use(['table', 'form', 'laypage', 'conf', 'utils', 'filters', 'dateFilters', 'mandyServices', 'store'], function (table, form, laypage, config, utils, filters, dateFilters, mandyServices, store) {
        var $searchKey = $(".search-key"),
            $searchBtn = $(".search-btn"),
            profile = store.get("profile");

        if (profile.institution.role.role == "主任") {
            $(".page-tools").append('<a class="layui-btn" href="#/schedule/referralStat">转诊统计</a>');
        }
        init = function () {
            getOutEMR(1, config.pagination.limit);
            getInEMR(1, config.pagination.limit);
        }
        init();

        /**
         * @author: ares
         * @date: 2021/1/20 15:03
         * @description: 搜索全部患者
         */
        $searchBtn.on('click', function () {
            var type = $('.layui-tab-title li.layui-this').data('type');
            if (type == 'out') {
                getOutEMR(1, config.pagination.limit);
            } else if (type == 'in') {
                getInEMR(1, config.pagination.limit);
            }
        })
        /**
         * @author: ares
         * @date: 2021/1/26 11:59
         * @description: 指定转入医院
         */
        top.layui.form.on('submit(transfer-in-hospital-submit)', function (data) {
            var fields = data.field,
                $form = $(data.form),
                inMedicalId = fields['transfer-in-hospital-select'],
                currentOption = $form.find('select[name="transfer-in-hospital-select"]').children('option[value="' + inMedicalId + '"]'),
                referralEMRId = currentOption.attr('referralId');
            if (referralEMRId && inMedicalId) assignEMRMedical(referralEMRId, inMedicalId);
            return false;
        });

        //转出患者查看病历
        function outEmrCheckInfo(data) {
            var query = {
                transferId: data.id,
                type: 0
            };
            utils.pushTopRoute('/emr/info/' + data.out_emr.id, query);
        }

        //转入患者查看病历
        function inEmrCheckInfo(data) {
            var query = {
                transfer_id: data.id,
                dis_edit: true,
                mes_emr: true,
                type: 1
            };
            utils.pushTopRoute('/emr/info/' + data.out_emr.id, query);
        }

        //转入患者且已建病历查看病历
        function inEmrCreatedCheckInfo(data) {
            utils.pushTopRoute('/emr/info/' + data.in_emr.id);
        }

        //转入患者新建转诊门诊
        function inEmrCreate(data) {
            utils.pushTopRoute('/referral/new/' + data.id);
        }

        /**
         * @author: ares
         * @date: 2021/1/26 11:19
         * @description: 显示指定转入医院模态框
         */
        function showTransferInHospitalModal(data) {
            var receiveDoctors = data.receiveDoctors,
                options = '<option value="">请选择</option>';
            if ($.type(receiveDoctors) == 'array') {
                $.each(receiveDoctors, function (index, item) {
                    var hospital = item.hospital,
                        medical = item.medical,
                        referral = item.referral_EMR;
                    options += '<option value="' + medical.id + '" referralId="' + referral.id + '">' + medical.name + "-" + hospital.name + '</option>';
                })
            }
            var content = '<form class="layui-form ares-p">' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">选择医院</label>' +
                '<div class="layui-input-block">' +
                '<select name="transfer-in-hospital-select" lay-verify="required">' + options + '</select>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item ares-w-100 ares-text-center ares-position-absolute ares-absolute-bl">' +
                '<button class="layui-btn layui-btn-sm" lay-submit lay-filter="transfer-in-hospital-submit">确定</button>' +
                '</div>' +
                '</form>';
            utils.layerOpen({
                type: 1,
                title: "指定转入医院",
                area: ['500px', '300px'],
                shade: 0.8,
                id: 'LAY_transfer-in-hospital',
                content: content,
                success: function () {
                    top.layui.form.render();
                }
            });
        }


        //转出患者
        function getOutEMR(currentPage, pageSize) {
            mandyServices.mandy_get_out_emr($searchKey.val(), currentPage, pageSize).then(function (res) {
                var data = res.data,
                    list = data.list || [];
                laypage.render($.extend(config.pagination, {
                    elem: 'pagination-out-patient',
                    count: data.count,
                    curr: data.current_page,
                    jump: function (obj, first) {
                        if (!first) getOutEMR(obj.curr, obj.limit);
                    }
                }));
                table.render({
                    elem: '#table-out-patient',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '患者编号', templet: '<span>{{d.out_emr.number}}</span>', width: 160},
                        {title: '姓名', templet: '<span>{{d.out_emr.patient.name}}</span>', width: 80,},
                        {
                            title: '性别',
                            width: 60,
                            templet: function (d) {
                                return filters.sexFormat(d.out_emr.patient.sex);
                            }
                        },
                        {
                            title: '年龄',
                            width: 60,
                            templet: function (d) {
                                return filters.ageFormat(d.out_emr.patient.birthday);
                            }
                        },
                        {
                            title: '医保类型',
                            width: 60,
                            templet: function (d) {
                                return filters.insuranceTypeFormat(d.out_emr.insurance_type);
                            }
                        },
                        {
                            title: '接收医院',
                            templet: function (d) {
                                return d.in_hospital ? d.in_hospital.name : '——';
                            }
                        },
                        {
                            title: '转诊状态',
                            width: 90,
                            templet: function (d) {
                                return '<b style="color: '+filters.transferStatusColorFormat(d.status)+'">'+filters.transferStatusFormat(d.status)+'</b>';
                            }
                        },
                        {
                            title: '转诊时间', width: 150,
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.out_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 120, templet: '#tableBar-transfer-out'}
                    ]]
                });
                table.on('tool(table-out-patient)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            outEmrCheckInfo(data);
                            break;
                        case 'assign':
                            showTransferInHospitalModal(data);
                            break;
                    }
                    ;
                });
            });

        }

        //转入患者
        function getInEMR(currentPage, pageSize) {
            mandyServices.mandy_get_in_emr($searchKey.val(), currentPage, pageSize).then(function (res) {
                var data = res.data,
                    list = data.list || [];
                laypage.render($.extend(config.pagination, {
                    elem: 'pagination-in-patient',
                    count: data.count,
                    curr: data.current_page,
                    jump: function (obj, first) {
                        if (!first) getInEMR(obj.curr, obj.limit);
                    }
                }));
                table.render({
                    elem: '#table-in-patient',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '患者编号', templet: '<span>{{d.out_emr.number}}</span>', width: 160},
                        {title: '姓名', templet: '<span>{{d.out_emr.patient.name}}</span>', width: 80,},
                        {
                            title: '性别',
                            width: 60,
                            templet: function (d) {
                                return filters.sexFormat(d.out_emr.patient.sex);
                            }
                        },
                        {
                            title: '年龄',
                            width: 60,
                            templet: function (d) {
                                return filters.ageFormat(d.out_emr.patient.birthday);
                            }
                        },
                        {
                            title: '医保类型',
                            width: 60,
                            templet: function (d) {
                                return filters.insuranceTypeFormat(d.out_emr.insurance_type);
                            }
                        },
                        {"title": '转出医院', templet: '<span>{{d.out_hospital.name}}</span>'},
                        {"title": '接诊医生', width: 80, templet: '<span>{{d.receive_medical.name}}</span>'},
                        {
                            title: '状态',
                            width: 90,
                            templet: function (d) {
                                return '<b style="color: '+filters.transferStatusColorFormat(d.status)+'">'+filters.transferStatusFormat(d.status)+'</b>';
                            }
                        },
                        {
                            title: '接诊时间', width: 150,
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.in_time);
                            }
                        },
                        {
                            title: '接诊完成时间', width: 150,
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.in_finish_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#tableBar-transfer-in'}
                    ]]
                });
                table.on('tool(table-in-patient)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            inEmrCheckInfo(data);
                            break;
                        case 'created':
                            inEmrCreatedCheckInfo(data);
                            break;
                        case 'create':
                            inEmrCreate(data);
                            break;
                    }
                    ;
                });
            });

        }

        //指定转入医院
        function assignEMRMedical(referralEMRId, inMedicalId) {
            mandyServices.mandy_put_receive_medical(referralEMRId, inMedicalId).then(function (res) {
                utils.msgSuccess(res.message);
                utils.layerCloseAll();
                getOutEMR(1, config.pagination.limit);
            });
        }

    })
</script>
</body>
</html>
